package com.tanhua.admin.module.dynamicmanage.controller;

import cn.hutool.core.util.ObjectUtil;
import com.tanhua.admin.module.dynamicmanage.service.impl.MessagesServiceImpl;
import com.tanhua.admin.module.dynamicmanage.vo.CommentPageResult;
import com.tanhua.admin.module.dynamicmanage.vo.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("manage/messages")
@Slf4j
public class MessagesController {

    @Autowired
    private MessagesServiceImpl messagesService;

    @GetMapping
    public ResponseEntity<PageResult> queryMessages(@RequestParam Map<String,Object> param){

        try {

            PageResult pageResult=messagesService.queryMessages(param);
            if(ObjectUtil.isNotEmpty(pageResult)){
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询消息信息失败~~");
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    @GetMapping("comments")
    public ResponseEntity<CommentPageResult> queryCommentList(@RequestParam Map<String,Object> param){
        try {

            CommentPageResult commentPageResult = messagesService.queryComment(param);
            if(ObjectUtil.isNotEmpty(commentPageResult)){
                return ResponseEntity.ok(commentPageResult);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("评论列表查询错误");
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    @PostMapping("revocation")
    public ResponseEntity<Map> undoAudit(@RequestBody String[] id){

        try {
            Map<String, String> message = messagesService.undoAudit(id);
            if(ObjectUtil.isNotEmpty(message)){
                return ResponseEntity.ok(message);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("撤销操作失败!");
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
